<template>
  <div class="system-user-dialog-container">
    <el-dialog v-model="state.dialog.isShowDialog" :title="state.dialog.title" width="769px">
      <el-form ref="dialogFormRef" :model="state.ruleForm" :rules="rules" label-width="90px" size="default">
        <el-row :gutter="35">
          <el-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24" class="mb20">
            <el-form-item label="参数名称" prop="configName">
              <el-input v-model="state.ruleForm.configName" :disabled="formReadonly" clearable
                        placeholder="请输入参数名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24" class="mb20">
            <el-form-item label="参数键名" prop="configKey">
              <el-input v-model="state.ruleForm.configKey" :disabled="formReadonly" clearable
                        placeholder="请输入参数键名"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24" class="mb20">
            <el-form-item label="系统内置" prop="configType">
              <el-switch v-model="state.ruleForm.configType" :disabled="formReadonly" active-text="是"
                         active-value="Y"
                         inactive-text="否" inactive-value="N" inline-prompt></el-switch>
            </el-form-item>
          </el-col>
          <el-col :lg="12" :md="24" :sm="24" :xl="24" :xs="24" class="mb20">
            <el-form-item label="参数键值" prop="configValue">
              <el-input v-model="state.ruleForm.configValue" :autosize="{ minRows: 2, maxRows: 4}"
                        placeholder="请输入参数键值"
                        type="textarea"/>
            </el-form-item>
          </el-col>
          <el-col :lg="24" :md="24" :sm="24" :xl="24" :xs="24" class="mb20">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="state.ruleForm.remark" :autosize="{ minRows: 2, maxRows: 4}"
                        placeholder="请输入备注信息"
                        type="textarea"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
				<span class="dialog-footer">
					<el-button size="default" @click="onCancel">取 消</el-button>
					<el-button size="default" type="primary" @click="onSubmit(dialogFormRef)">
						{{ state.dialog.submitTxt }}
					</el-button>
				</span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" name="configDialog" setup>
import {computed, nextTick, reactive, ref} from 'vue';
import type {FormInstance, FormRules} from 'element-plus';
import {ElMessage} from 'element-plus';
import {configApi} from "/@/api/sys/config";
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const dialogFormRef = ref();
const state = reactive({
  ruleForm: {
    id: '',
    configName: '',
    configKey: '',
    configValue: '',
    configType: '',
    remark: '',
  },
  dialog: {
    isShowDialog: false,
    type: '',
    title: '',
    submitTxt: '',
  },
});


const rules = reactive<FormRules>({
  configName: [{required: true, message: '不能为空', trigger: 'blur'}],
  configKey: [{required: true, message: '不能为空', trigger: 'blur'}],
  configValue: [{required: true, message: '不能为空', trigger: 'blur'}],
});
// 打开弹窗
const openDialog = (type: string, row: any) => {
  state.dialog.type = type;
  state.dialog.isShowDialog = true;
  if (type === 'edit') {
    state.dialog.title = '修改配置';
    state.dialog.submitTxt = '修 改';
  } else if (type === 'add') {
    state.dialog.title = '新增配置';
    state.dialog.submitTxt = '新 增';
  }

  nextTick(() => {
    if (type === 'add') {
      state.ruleForm.id = '';
      // 清空表单，此项需加表单验证才能使用
      dialogFormRef.value.resetFields();
    } else {
      state.ruleForm = Object.assign(state.ruleForm, row);
    }
  });
};


// 关闭弹窗
const closeDialog = () => {
  state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
  closeDialog();
};
// 提交
const onSubmit = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      if (state.dialog.type === 'add') {
        configApi()
            .addRecord(state.ruleForm)
            .then((res) => {
              if (res && res.code === 200) {
                ElMessage.success(res.msg || '新增成功');
                closeDialog();
                emit('refresh');
              }
            });
      } else if (state.dialog.type === 'edit') {
        configApi()
            .updateRecord(state.ruleForm)
            .then((res) => {
              if (res && res.code === 200) {
                ElMessage.success(res.msg || '修改成功');
                closeDialog();
                emit('refresh');
              }
            });
      }
    }
  });
};
const formReadonly = computed(() => {
  return state.dialog.type !== 'add' && state.dialog.type !== 'edit'
})
// 暴露变量
defineExpose({
  openDialog,
});
</script>
